/** 打击者1945初代

  By setycyas @2024-12-27
*/

/* 全局设定
*/ {
#Warn
#MaxThreadsPerHotkey 1
SetWorkingDir(A_ScriptDir)
#Include "..\Class\FaceDetector.2.ahk"
#Include "..\Class\CommonMacro.2.ahk"
#Include "..\Class\CommonUI.2.ahk"
#Include "..\Class\MyFuns.2.ahk"
#Include "..\Class\MyEmu.2.ahk"
#Include "..\Class\OrderMap.2.ahk"
;SendMode("Event")
SendMode("Input")
}

/* 制作打击者1945的宏类
*/ class S1945Macro extends CommonMacro {

  pressDuration := 30
  aFlag := False ;a键按下与否的标记

  /* 函数
  */
  ;需要蓄力delay时间的蓄力攻击
  _蓄力攻击(delay){
    if (this.aFlag) {
      this._up(1)
      sleep(30)
      this.aFlag := True
    }
    this._down(1)
    this.aFlag := True
    sleep(delay)
    this._up(1)
    this.aFlag := False
  }

  /* 新增的宏函数
  */
  ;改装一下连发A,当被蓄力中断后判断一下按键的放开状态
  _连发A(key){
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    while (MyFuns.getVkState(vk)) {
      this._down(1)
      this.aFlag := True
      sleep(this.pressDuration)
      if (this.aFlag) {
        this._up(1)
        this.aFlag := False
      }
      sleep(this.autoFireDelay)
    }
  }
  ;各机体的蓄力
  _蓄力1(key){
    this._蓄力攻击(520)
  }
  _蓄力2(key){
    this._蓄力攻击(1520)
  }
  _蓄力3(key){
    this._蓄力攻击(600)
  }
  _蓄力4(key){
    this._蓄力攻击(860)
  }
  _蓄力5(key){
    this._蓄力攻击(680)
  }
  _蓄力6(key){
    this._蓄力攻击(1200)
  }
  ;轻微位移用
  _右微移(key){
    MyFuns.press("right", 20)
    sleep(50)
  }
  _左微移(key){
    MyFuns.press("left", 20)
    sleep(50)
  }
  ;fbneo64的窗口变换,整个窗体长宽比大概是0.7
  _FBNEO64窗口变换(key){
    static W_TITLE := "ahk_exe fbneo64.exe"
    static HEIGHT := 768
    static WIDTH := Round(HEIGHT*0.7)
    static X:= Round((1366-WIDTH)/2)-100
    ;执行
    MyFuns.setWindow(W_TITLE, WIDTH, HEIGHT, X, 0)
  }
  ;游聚屏幕变换
  _游聚Tab屏变换(key){  
    static W_TITLE := "(setycyas) ahk_exe x-zone.exe"
    ;自定的两边留空和标题栏宽度,窗口参数
    static SIDE := 20
    static TITLE_BAR := 76
    static HEIGHT := 768+(TITLE_BAR-40)
    static WIDTH := Round(768*0.7)+SIDE*2
    static X:= Round((1366-WIDTH)/2)-100
    ;执行
    MyFuns.setWindow(W_TITLE, WIDTH, HEIGHT, X, -(TITLE_BAR-40))
  }
  _游聚Tab屏还原(key){  
    static W_TITLE := "(setycyas) ahk_exe x-zone.exe"
    static HEIGHT := 768
    static WIDTH := Round(768*0.7)
    ;执行
    MyFuns.setWindow(W_TITLE, WIDTH, HEIGHT, 0, 0)
  }

  /* 重载宏接口
  */
  _setMacro(){
    static obm := ObjBindMethod ;函数名简写,制作对象绑定函数
    local i, v
    ;制作宏相关Map
    this.macroMap := OrderMap()
    this.macroMap["连发A"] := obm(this, "_连发A")
    this.macroMap["左微移"] := obm(this, "_左微移")
    this.macroMap["右微移"] := obm(this, "_右微移")
    this.macroMap["FBNEO64窗口变换"] := obm(this, "_FBNEO64窗口变换")
    this.macroMap["游聚Tab屏变换"] := obm(this, "_游聚Tab屏变换")
    this.macroMap["游聚Tab屏还原"] := obm(this, "_游聚Tab屏还原")
    this.macroMap["空函数"] := obm(this, "_空函数")
    ;1-6号机的蓄力
    for i, v in MyFuns.range(1, 6) {
      this.macroMap["蓄力" . v] := obm(this, "_蓄力" . v)
    }
    ;制作指定宏顺序的数组
    this.macroArr := this.macroMap.getKeys()
  }
}

/* 制作打击者1945专用UI
*/
class S1945UI extends CommonUI {

  HOTKEY_NUM := 7
  EXE_ARRAY := [MyEmu.FBNEO64, MyEmu.MAME, MyEmu.XZONE]

  /* 构造函数
  */
  __New(macro){
    super.__New("打击者1945", macro)
    local i, v
    this._preset := OrderMap()
    this._macro.UI := this ;绑定UI
    ;设置preset和提升蓄力宏的优先级
    for i, v in MyFuns.range(1, 6) {
      this._preset[v . "号机"] := ["连发A", "蓄力" . v, "左微移", 
        "右微移", "FBNEO64窗口变换", "空函数"
      ]
      this.MACRO_PRIORITY["蓄力" . v] := "p1"
    }
  }

  /* 重写接口
  */

  /* 重写setup,更改可选的exe,读入预设
  */
  setup(){
    super.setup()
    this._myGui.opt("+AlwaysOnTop")
    this._myGui.show("x1100 y230")
    this.readPresetMenu("4号机")
  }
  
  ;自定义gui组件标签页,增加一个挡板召唤键
  /*
  _customGui(){
    super._customGui()
    ;增加召唤标尺窗口按键
    this._rulerButton := this._myGui.Add("Button", "Default w200", "召唤标尺")
    this._rulerButton.OnEvent(
      "Click", 
      ObjBindMethod(this, "_onRulerButtonClick")
    )
  }
  */

  /* 自定义函数
  */
  ;召唤标尺窗口
  /*
  _onRulerButtonClick(btnObj, info){
    static ruler := ""
    static wSize := [130, 700]
    static infos := [
      [307, "00FF00", "绿色魔法师"],
      [380, "CC00CC", "最后一关虫子"]
    ]
    static x := 70
    
    if ruler {
      ruler._gui.Restore()
    } else {
      ruler := RulerWindow(wSize, infos, x)
      ruler.show("x240 y0")
    }
  }
  */

}

/* 运行当前脚本
*/ if (A_ScriptName == "打击者1945一代.2.ahk") {
  iconFile := "..\Icon\战国之刃小夜.png"
  TraySetIcon(iconFile, , 1)

  face := FaceDetector() ;方向检测对象
  macro := S1945Macro(face, ["q","f"]) ;宏对象,有界面时第二个参数可以随便写但长度不要小于2
  ;新建界面对象并显示
  ui := S1945UI(macro)
  ui.setup()
  ;更新组件内容
  ;ui._exeComboBox.Choose(MyEmu.XZONE)
  ui._exeComboBox.Choose(MyEmu.FBNEO64)
  ui._gameKeyEdit.Text := "a,space"
  ui._hotkeyEdit.Text := "~s,~d,~e,~f,~o,~l"
  ui._fpsEdit.Text := 10
  ui._testEdit.Text := "1200,20,30,30"
  ui.readPresetMenu("4号机")
}
